package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsDept;
import com.macro.mall.service.UmsDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构表(ums_dept）
 *
 * @author gaodx
 * @create 2020-08-11 15:43
 **/
@Slf4j
@RestController
@RequestMapping("umsDept")
@Api(tags = "UmsDeptController", description = "机构表(ums_dept）")
public class UmsDeptController {


    @Autowired
    private UmsDeptService umsDeptService;


    @ApiOperation("机构列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<UmsDept>> list(
            @RequestParam(value = "deptName", required = false) String deptName,
            @RequestParam(value = "deptType", required = false) Integer deptType,
            @RequestParam(value = "deptRange", required = false) String deptRange,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<UmsDept> umsDept = umsDeptService.selectAllByUmsDeptAndRange(deptName, deptType, deptRange, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(umsDept));
    }


    @ApiOperation("添加机构")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody UmsDept request) {
        try {
            umsDeptService.saveUmsDeptSelective(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            log.error("", e);
        }
        return CommonResult.failed();

    }

    @ApiOperation("查询机构详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) String id) {
        try {
            UmsDept umsDept = umsDeptService.selectUmsDeptById(id);
            return CommonResult.success(umsDept);
        } catch (Exception e) {
            log.error("", e);
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除机构信息")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteMenberById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) String id) {
        try {
            UmsDept umsDept = new UmsDept();
            umsDept.setId(Integer.parseInt(id));
            umsDept.setStatus(0);
            umsDeptService.updateStatus(umsDept);
            return CommonResult.success(null);
        } catch (Exception e) {
            log.error("", e);
        }
        return CommonResult.failed();

    }

    @ApiOperation("更新机构信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@ApiParam(value = "请求报文") @RequestBody UmsDept request) {
        try {
            umsDeptService.updateUmsDeptSelective(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            log.error("", e);
        }
        return CommonResult.failed();

    }


}